import domain.User;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.embedded.EmbeddedChannel;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

@Slf4j
public class EmbeddedChannelTest4 {

    public static void main(String[] args) {

        ChannelInboundHandlerAdapter h1 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("h1");

                User user = new User(1L, "root");
                super.channelRead(ctx, user);
            }
        };

        SimpleChannelInboundHandler<User> h2 = new SimpleChannelInboundHandler<User>() {

            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                System.out.println("h2-------");
                super.channelRead(ctx, msg);
            }

            @Override
            protected void channelRead0(ChannelHandlerContext ctx, User msg) throws Exception {
                log.info("h2");
//                super.channelRead(ctx, msg);
            }
        };

        ChannelInboundHandlerAdapter h3 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("h3");
                super.channelRead(ctx, msg);
            }
        };

        SimpleChannelInboundHandler<User> h4 = new SimpleChannelInboundHandler<User>() {

//            @Override
//            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//                log.info("h44444444444444");
//                super.channelRead(ctx, msg);
//            }

            @Override
            protected void channelRead0(ChannelHandlerContext ctx, User msg) throws Exception {
                log.info("h4");
                super.channelRead(ctx, msg);
            }
        };

        //添加handler并且活的channel
        EmbeddedChannel channel = new EmbeddedChannel(h1, h2, h3, h4);

        //模拟入栈操作
        channel.writeInbound(ByteBufAllocator.DEFAULT.buffer().writeBytes("Hello".getBytes(StandardCharsets.UTF_8)));

//        模拟出栈操作
//        channel.writeOutbound(ByteBufAllocator.DEFAULT.buffer().writeBytes("模拟出栈内容".getBytes(StandardCharsets.UTF_8)));


    }
}
